$(function() {
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    let months = [];
    let current_month = '';
    let current_combo_uuid = '';
    let app_store_product_id = '';
    let coupon_uuid = '';
    let start_month = '';
    let sw = '';
    let platform = GetQueryString('platform');
    let token = GetQueryString('token');
    let version = '';
    if (GetQueryString('version')) {
        version = GetQueryString('version');
    }
    let version_num = Number(version.replace(/\./g,''));
    if (platform === 'ios') {
        $('.coupon_box').eq(0).css('display','none');
    }
    $.ajax({
        type: "GET",
        timeout: 15000,
        url: "https://kksappapi.kekeshi.com/api/app/v1/mommy_school/combo_orders/choose_combo?token=" + token + "&platform=" + platform + '&version=' + version,
        // url: "http://121.196.207.137:3000/api/app/v1/mommy_school/combo_orders/choose_combo?token=" + token + "&platform=" + platform + '&version=' + version,
        dataType: "json",
        data: {},
        success: function(response) {
            if (response.code === 200) {
            var data = response.data;
            var str = '';
            let eq = -1;
            start_month = data.start_month;
            current_combo_uuid = data.combos[0].uuid;
            current_month = data.start_month;
            app_store_product_id = data.app_store_product_id;
            coupon_uuid = data.coupon.coupon_uuid;
            sw = data.sw;
            // 注意：输出的i与obj分别为下标与单个的对象
            $.each(data.combos, function(i, obj) {
                str += '<div class="one_year" data-uuid="' + obj.uuid + '" onclick="render_page(this)">';
                if (obj.hot === true) {
                    str += '<img src="' + "images/mamibuy_lable_tuijian.png" + '" alt="" class="tag_img">';
                }
                str += '<div class="top_title_price">';
                str += '<div class="course_title">' + obj.title + '</div>';
                str += '<div class="price_box">';
                // if (version_num < 203){
                if (obj.original_price !== '' && obj.original_price !== null){
                    str += '<div class="original"> &nbsp;&nbsp;原价&yen;'+ obj.original_price +'&nbsp;&nbsp; </div>';
                }
                str += '<div class="price">&yen;' + obj.price + '</div>';
                str += '</div>';
                str += '</div>';
                if (obj.courses.length !== 0) {
                    str += '<div class="center_content_box">';
                    $.each(obj.courses, function (n, course_obj) {
                        str += '<div class="content_box">';
                        str += '<div class="content_title">' + course_obj.name + '</div>';
                        str += '<div class="content_number">' + course_obj.count + '节</div>';
                        str += '</div>';
                    });
                    str += '<div style="clear:both;"></div>';
                    str += '</div>';
                    str += '<div class="course_desc">共计' + obj.course_count + '节课，每节低至' + obj.avg_price + '元</div>';
                }
                str += '</div>';
                str += '</div>';
                if (obj.choose === true) {
                    eq = i;
                }
            });
            $(".course").html(str);
            $('.one_year').eq(eq).css('border','1px solid #ff7d7d');
            $('.one_year').eq(eq).css('background','#FFF2F2');
            $('.center_content_box').eq(eq).css('background','#ffffff');
            $('.center_content_box').eq(eq).find('.content_number').css('background','#fff2f2');
            $('.course_desc').eq(eq).css('color','#ff7d7d');

            // 月份
            months = [];
            $.each(data.month_table, function(i, obj) {
                if (obj.state === 1) {
                    $('.month').eq(i).css('background','#ffc2c2');
                    $('.month').eq(i).css('color','#ffffff');
                } else if (obj.state === 2) {
                    $('.month').eq(i).css('background','#ff7d7d');
                    $('.month').eq(i).css('color','#ffffff');
                    months.push(i + 1);
                }
            });
            $('.coupon_title').eq(0).html('已使用代金券：&yen;' + data.coupon.amount)
            $('.bottom_price').eq(0).html('&yen;' + data.total_fee)
            //
            if (data.start_month <= 36) {
                gen_buy_url();
            } else {
                $('.center_content_box').css('display','none')
                $('.course_desc').css('display','none')
            }
            if (data.alert != "") {
                $('.alert_bg').css('display','block');
                $('.alert_text').html(data.alert);
            }
            //去购买显示隐藏
                console.log(sw)
                if(sw === 'o'){
                    $('.go_buy_btn').css('display','none')
                } else {
                    $('.go_buy_btn').css('display','block')
                }
            this.clicked = false;
        } else if (response.code === 401) {
                $('.error_bg').css('display','block');
                $('.error_text').html(response.msg);
            }
        },
        complete: function (XMLHttpRequest,status) {
            console.log(status)
            if (status == 'timeout') {
                $('.alert_text').html('请求已超时');
                $('.alert_bg').css('display','block');
            }
        }
    });
    $('.month').on('click',function(){
        if (sw === 'c') {
            return false;
        }
        //去购买显示隐藏
        console.log(sw)
        if(sw === 'o'){
            $('.go_buy_btn').css('display','none')
        } else {
            $('.go_buy_btn').css('display','block')
        }
        current_month = $(this).text();
        console.log(current_month);
        $.ajax({
            type: "GET",
            timeout: 15000,
            url: "https://kksappapi.kekeshi.com/api/app/v1/mommy_school/combo_orders/choose_combo?token=" + token + "&platform=" + platform + '&month=' + current_month + '&combo_uuid=' + current_combo_uuid + '&version=' + version,
            // url: "http://121.196.207.137:3000/api/app/v1/mommy_school/combo_orders/choose_combo?token=" + token + "&platform=" + platform + '&month=' + current_month + '&combo_uuid=' + current_combo_uuid + '&version=' + version,
            dataType: "json",
            data: {},
            success: function(response) {
                console.log(response.data);
                if (response.code === 200) {
                    var data = response.data;
                    var str = '';
                    let eq = -1;
                    start_month = data.start_month;
                    coupon_uuid = data.coupon.coupon_uuid;
                    sw = data.sw;
                    // 注意：输出的i与obj分别为下标与单个的对象
                    $.each(data.combos, function (i, obj) {
                        str += '<div class="one_year" data-uuid="' + obj.uuid + '" onclick="render_page(this)">';
                        if (obj.hot === true) {
                            str += '<img src="' + "images/mamibuy_lable_tuijian.png" + '" alt="" class="tag_img">';
                        }
                        str += '<div class="top_title_price">';
                        str += '<div class="course_title">' + obj.title + '</div>';
                        str += '<div class="price_box">';
                        // if (version_num < 203){
                        if (obj.original_price !== '' && obj.original_price !== null){
                            str += '<div class="original"> &nbsp;&nbsp;原价&yen;'+ obj.original_price +'&nbsp;&nbsp; </div>';
                        }
                        str += '<div class="price">&yen;' + obj.price + '</div>';
                        str += '</div>';
                        str += '</div>';
                        if (obj.courses.length !== 0) {
                            str += '<div class="center_content_box">';
                            $.each(obj.courses, function (n, course_obj) {
                                str += '<div class="content_box">';
                                str += '<div class="content_title">' + course_obj.name + '</div>';
                                str += '<div class="content_number">' + course_obj.count + '节</div>';
                                str += '</div>';
                            });
                            str += '<div style="clear:both;"></div>';
                            str += '</div>';
                            str += '<div class="course_desc">共计' + obj.course_count + '节课，每节低至' + obj.avg_price + '元</div>';
                        }
                        str += '</div>';
                        str += '</div>';
                        if (obj.choose === true) {
                            eq = i;
                        }
                    });
                    $(".course").html(str);
                    $('.one_year').eq(eq).css('border', '1px solid #ff7d7d');
                    $('.one_year').eq(eq).css('background', '#FFF2F2');
                    $('.center_content_box').eq(eq).css('background', '#ffffff');
                    $('.center_content_box').eq(eq).find('.content_number').css('background', '#fff2f2');
                    $('.course_desc').eq(eq).css('color', '#ff7d7d');

                    // 月份
                    months = [];
                    $('.month').css('background', 'none')
                    $('.month').css('color', '#333333')
                    $.each(data.month_table, function (i, obj) {
                        if (obj.state === 1) {
                            $('.month').eq(i).css('background', '#ffc2c2');
                            $('.month').eq(i).css('color', '#ffffff');
                        } else if (obj.state === 2) {
                            $('.month').eq(i).css('background', '#ff7d7d');
                            $('.month').eq(i).css('color', '#ffffff');
                            months.push(i + 1);
                        }
                    });
                    $('.coupon_title').eq(0).html('已使用代金券：&yen;' + data.coupon.amount);
                    $('.bottom_price').eq(0).html('&yen;' + data.total_fee);
                    gen_buy_url();
                    if (data.alert != "") {
                        $('.alert_bg').css('display', 'block');
                        $('.alert_text').html(data.alert);
                    }
                    this.clicked = false;
                } else if (response.code === 401) {
                    $('.error_bg').css('display','block');
                    $('.error_text').html(response.msg);
                }
            },
            complete: function (XMLHttpRequest,status) {
                console.log(status)
            if (status == 'timeout') {
                $('.alert_text').html('请求已超时');
                $('.alert_bg').css('display','block');
            }
        }
        });
    });
    render_page = function(e) {
        current_combo_uuid = $(e).attr('data-uuid');
        $.ajax({
            type: "GET",
            timeout: 15000,
            url: "https://kksappapi.kekeshi.com/api/app/v1/mommy_school/combo_orders/choose_combo?token=" + token + "&platform=" + platform + '&month=' + current_month + '&combo_uuid=' + current_combo_uuid + '&version=' + version,
            // url: "http://121.196.207.137:3000/api/app/v1/mommy_school/combo_orders/choose_combo?token=" + token + "&platform=" + platform + '&month=' + current_month + '&combo_uuid=' + current_combo_uuid + '&version=' + version,
            dataType: "json",
            data: {},
            success: function(response) {
                console.log(response.data);
                if(response.code === 200) {
                var data = response.data;
                var str = '';
                let eq = -1;
                start_month = data.start_month;
                coupon_uuid = data.coupon.coupon_uuid;
                app_store_product_id = data.app_store_product_id;
                sw = data.sw;
                // 注意：输出的i与obj分别为下标与单个的对象
                $.each(data.combos, function(i, obj) {
                    str += '<div class="one_year" data-uuid="' + obj.uuid + '" onclick="render_page(this)">';
                    if (obj.hot === true) {
                        str += '<img src="' + "images/mamibuy_lable_tuijian.png" + '" alt="" class="tag_img">';
                    }
                    str += '<div class="top_title_price">';
                    str += '<div class="course_title">' + obj.title + '</div>';
                    str += '<div class="price_box">';
                    // if (version_num < 203){
                    if (obj.original_price !== '' && obj.original_price !== null){
                        str += '<div class="original"> &nbsp;&nbsp;原价&yen;'+ obj.original_price +'&nbsp;&nbsp; </div>';
                    }
                    str += '<div class="price">&yen;' + obj.price + '</div>';
                    str += '</div>';
                    str += '</div>';
                    if (obj.courses.length !== 0) {
                        str += '<div class="center_content_box">';
                        $.each(obj.courses, function(n, course_obj) {
                            str += '<div class="content_box">';
                            str += '<div class="content_title">' + course_obj.name + '</div>';
                            str += '<div class="content_number">' + course_obj.count + '节</div>';
                            str += '</div>';
                        });
                        str += '<div style="clear:both;"></div>';
                        str += '</div>';
                        str += '<div class="course_desc">共计' + obj.course_count + '节课，每节低至' + obj.avg_price + '元</div>';
                    }
                    str += '</div>';
                    str += '</div>';
                    if (obj.choose === true) {
                        eq = i;
                    }
                });
                $(".course").html(str);
                $('.one_year').eq(eq).css('border','1px solid #ff7d7d');
                $('.one_year').eq(eq).css('background','#FFF2F2');
                $('.center_content_box').eq(eq).css('background','#ffffff');
                $('.center_content_box').eq(eq).find('.content_number').css('background','#fff2f2');
                $('.course_desc').eq(eq).css('color','#ff7d7d');

                // 月份
                months = [];
                $('.month').css('background','none');
                $('.month').css('color','#333333');
                $.each(data.month_table, function(i, obj) {
                    if (obj.state === 1) {
                        $('.month').eq(i).css('background','#ffc2c2');
                        $('.month').eq(i).css('color','#ffffff');
                    } else if (obj.state === 2) {
                        $('.month').eq(i).css('background','#ff7d7d');
                        $('.month').eq(i).css('color','#ffffff');
                        months.push(i + 1);
                    }
                });
                $('.coupon_title').eq(0).html('已使用代金券：&yen;' + data.coupon.amount);
                $('.bottom_price').eq(0).html('&yen;' + data.total_fee);
                if (data.alert != "") {
                    $('.alert_bg').css('display','block');
                    $('.alert_text').html(data.alert);
                }
                gen_buy_url();
                    //去购买显示隐藏
                    console.log(sw)
                    if(sw === 'o'){
                        $('.go_buy_btn').css('display','none')
                    } else {
                        $('.go_buy_btn').css('display','block')
                    }
                this.clicked = false;
                } else if (response.code === 401) {
                    $('.error_bg').css('display','block');
                    $('.error_text').html(response.msg);
                }
            },
            complete: function (XMLHttpRequest,status) {
                console.log(status)
                if (status == 'timeout') {
                    $('.alert_text').html('请求已超时');
                    $('.alert_bg').css('display','block');
                }
            }
        });
    };
    go_chat = function() {
        location.href = 'kekeshi://app.kekeshi.com/CustomerService';
    };
    gen_buy_url = function () {
        console.log(months.toString());
        $("#buy_link").attr("href", 'kekeshi://app.kekeshi.com/orders/single_create?app_store_product_id=' + app_store_product_id + '&item_uuid=' + current_combo_uuid + '&months=' + months.toString() + '&coupon_uuid=' + coupon_uuid);
        console.log($('#buy_link').attr('href'))
    };
    not_repeat = (event) => {
        if (start_month > 36) {
            $('.tips').css('display', 'block');
            setTimeout(function(){
                $('.tips').css('display', 'none');
            },5000);
        }
        if (this.clicked) {
            if(event.preventDefault){
                event.preventDefault();
            }else{
                event.returnValue =false;
            }
        } else {
            this.clicked = true;
        }
    };
    close_alert = () => {
      $('.alert_bg').css('display','none');
      $('.alert_text').html('');
    }
});
